package cn.blnp.net.flowable.identity.group;

import lombok.extern.slf4j.Slf4j;
import org.flowable.engine.IdentityService;
import org.flowable.idm.api.Group;
import org.flowable.idm.api.GroupQuery;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

import javax.annotation.Resource;
import java.util.List;

/**
 * <h3>用户组管理案例演示</h3>
 *
 * @author <a href="mailto:blnp.yibin@qq.com">lyb</a>
 * @version 1.0
 * @since 2025/4/30 14:12
 */
@Slf4j
@SpringBootTest
public class FlowGroupTest {

    @Resource
    private IdentityService identityService;

    /**
     * <p><b>用途:新建用户组<b></p>
     * @author <a href="mailto:blnp.yibin@qq.com">lyb</a>
     * @since 14:14 2025/4/30
     * @params [id, name, type] 
     * @param id 用户组编号
     * @param name 用户组名称
     * @param type 用户组类型
     * @return void
    **/
    public void addGroup(String id, String name, String type) {
        //调用IdentityService的newGroup()方法创建Group实例
        Group newGroup = identityService.newGroup(id);
        //调用setter()方法为创建的User实例设置属性值
        newGroup.setName(name);
        newGroup.setType(type);
        //调用IdentityService的saveGroup()方法将Group实例保存到数据库中
        identityService.saveGroup(newGroup);
    }
    
    @Test
    public void test() {
        addGroup("process_platform_department", "流程平台部", "department");

        //查询匹配的用户组并打印
        GroupQuery groupQuery = identityService.createGroupQuery()
                .groupType("department")
                .orderByGroupId()
                .asc();
        List<Group> list = groupQuery.list();
        for (Group group : list) {
            log.info("用户组编号：{}，名称：{}，类型：{}", group.getId(), group.getName(), group.getType());
        }

        /**
         *  演示结果：
         *  2025-04-30 14:19:34.839  INFO 71948 --- [           main] c.b.n.f.identity.group.FlowGroupTest     : 用户组编号：process_platform_department，名称：流程平台部，类型：department
         **/
    }
}
